<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn ">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />


  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>视频投放 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>



  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
    include="jquery,jquery.range,font-awesome,bootstrap,bootstrap-checkbox,layer,haoutil,turf,mars3d"></script>

  <link href="css/style.css" rel="stylesheet" />
  <style>
    .infoview input {
      vertical-align: middle;
      padding-top: 2px;
      padding-bottom: 2px;
    }
  </style>
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>


  <!-- 面板 -->
  <div class="infoview" id='toolbar'>
    <input id="btnAddVideo" type="button" class="btn btn-primary" value="投射视频" />
    <input id="btnClear" type="button" class="btn btn-info" value="清除" />


    <table class="mars-table">
      <tbody>
        <tr>
          <td>视角水平张角</td>
          <td>
            <input type="range" min="1" max="179" step="1" data-bind="value: fov, valueUpdate: 'input'">
          </td>
          <td>
            <input type="number" style="width: 70px" size="5" data-bind="value: fov">
          </td>
        </tr>
        <tr>
          <td>视角宽高比例</td>
          <td>
            <input type="range" min="1" max="3.0" step="0.2" data-bind="value: aspectRatio, valueUpdate: 'input'">
          </td>
          <td>
            <input type="number" style="width: 70px" size="5" data-bind="value: aspectRatio">
          </td>
        </tr>
        <tr>
          <td>视角距离</td>
          <td>
            <input id="viewDis" type="range" min="1" max="200" step="1" data-bind="value: dis, valueUpdate: 'input'">

          </td>
          <td>
            <input id="viewDis1" type="number" style="width: 70px" size="5" data-bind="value: dis">
          </td>
        </tr>
        <tr>
          <td>视频位置:</td>
          <td>
            <input id="btnSelCamera" type="button" class="btn btn-info" value="图上点选" />
          </td>
          <td>
            默认为当前相机位置
          </td>
        </tr>

        <tr>
          <td>键盘微调:</td>
          <td>
            步长<input id="adjustDeg" type="number" value="0.5" min="0.001" max="2" class="form-control"
              style="width: 90px;" />度
          </td>

          <td>
            <div class="checkbox checkbox-info checkbox-inline">
              <input id="chkCameraFollow" type="checkbox" class="checkbox checkbox-info checkbox-inline" value="相机跟随" />
              <label for="chkCameraFollow">相机跟随</label>
            </div>
          </td>
        </tr>
        <tr>
          <td>视频角度</td>
          <td>
            <input id="rotateDeg" type="range" min="0" max="360" step="1" value='0'>
          </td>
        </tr>

        <tr>
          <td>视椎框线:</td>
          <td>
            <div class="checkbox checkbox-info checkbox-inline">
              <input id="showFrustum" checked type="checkbox" class="checkbox checkbox-info checkbox-inline"
                value="true" />
              <label for="showFrustum">是否显示</label>
            </div>
          </td>
        </tr>

      </tbody>
    </table>

    <input type="button" class="btn btn-info" value="播放暂停" onclick="playOrpause()" />
    <input id="btnLocate" type="button" class="btn btn-info" value="定位至视频位置" />
    <input id="console" type="button" class="btn btn-info" value="打印参数" />
  </div>

  <video id="trailer" style="display: none;" muted="muted" autoplay="autoplay" loop="loop" crossorigin="" controls="">
    <!-- <source src="http://data.marsgis.cn/video/lukou.mp4" type="video/mp4"> -->
  </video>

  <script src="../lib/video/hls/hls.js"></script>

  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map
    var videoElement
    var selectedView

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          //此处参数会覆盖config.json中的对应配置
          center: {
            y: 31.843475,
            x: 117.205447,
            z: 107.25,
            heading: 175.1,
            pitch: -25.7,
            roll: 0,
          },
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      //添加参考三维模型
      var tiles3dLayer = new mars3d.layer.TilesetLayer({
        name: '合肥国家大学科技园',
        url: 'http://data.marsgis.cn/3dtiles/qx-hfdxy/tileset.json',
        position: { alt: -19 },
        maximumScreenSpaceError: 2,
        maximumMemoryUsage: 2048,
        dynamicScreenSpaceError: true,
        cullWithChildrenBounds: false,
        luminanceAtZenith: 0.6,
      })
      map.addLayer(tiles3dLayer)

      //创建矢量数据图层
      var graphicLayer = new mars3d.layer.GraphicLayer()
      map.addLayer(graphicLayer)

      //2.在layer上绑定监听事件
      graphicLayer.on(mars3d.EventType.click, function (event) {
        selectedView = event.graphic
        console.log('监听layer，单击了矢量对象', event)
      })
      // graphicLayer.on(mars3d.EventType.mouseOver, function(event) {
      //   console.log('监听layer，鼠标移入了矢量对象', event)
      // })
      // graphicLayer.on(mars3d.EventType.mouseOut, function(event) {
      //   console.log('监听layer，鼠标移出了矢量对象', event)
      // })

      //可在图层上绑定popup,对所有加到这个图层的矢量数据都生效
      graphicLayer.bindPopup('我是layer上绑定的Popup', {
        anchor: [0, -10],
      })

      //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效
      // graphicLayer.bindTooltip('我是layer上绑定的Tooltip')

      //可在图层绑定右键菜单,对所有加到这个图层的矢量数据都生效
      graphicLayer.bindContextMenu([
        {
          text: '删除对象',
          iconCls: 'fa fa-trash-o',
          callback: function (e) {
            var graphic = e.graphic
            if (graphic) {
              graphicLayer.removeGraphic(graphic)
            }
          },
        },
      ])

      //绑定属性及处理事件
      bindViewModel()
      bindEvnet()

      // var hlsUrl = "http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8"
      var hlsUrl = 'http://ivi.bupt.edu.cn/hls/cctv13.m3u8'

      videoElement = document.getElementById('trailer')

      if (window.Hls.isSupported()) {
        var hls = new window.Hls()
        hls.loadSource(hlsUrl)
        hls.attachMedia(videoElement)
        hls.on(window.Hls.Events.MANIFEST_PARSED, function () {
          videoElement.play()
        })
      } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) {
        videoElement.src = hlsUrl
        videoElement.addEventListener('loadedmetadata', function () {
          videoElement.play()
        })
      }

      //加一些演示数据
      addGraphic_01(graphicLayer)

      $('#btnAddVideo').click(function () {
        addGraphic_Video(graphicLayer)
      })

      //清除图层
      $('#btnClear').click(function () {
        graphicLayer.clear()
        selectedView = null
      })
    }

    // 加载已配置好的视频（此参数为界面上“打印参数”按钮获取的）
    function addGraphic_01(graphicLayer) {
      var video2D = new mars3d.graphic.Video2D({
        dom: videoElement,
        style: {
          camera: {
            direction: { x: -0.20300781957546601, y: 0.3881445982693198, z: -0.8989613985180693 },
            up: { x: -0.41112481743883666, y: 0.7994469141644973, z: 0.43801942413407347 },
            right: { x: 0.8886867894129509, y: 0.4585067090754624, z: -0.0027180978075245542 },
          },
          dis: 70,
          fovDegree: 52,
          aspectRatio: 3,
          stRotationDegree: 0,
        },
        showFrustum: true,
      })
      graphicLayer.addGraphic(video2D)

      selectedView = video2D //记录下
    }

    function addGraphic_Video(graphicLayer) {
      var video2D = new mars3d.graphic.Video2D({
        dom: videoElement,
        style: {
          aspectRatio: map.scene.camera.frustum.aspectRatio,
          fov: map.scene.camera.frustum.fov,
          dis: viewModel.dis,
        },
      })
      graphicLayer.addGraphic(video2D)

      selectedView = video2D //记录下
    }

    //面板参数更新
    var viewModel = {
      fov: 50,
      aspectRatio: 2.0,
      dis: 70,
      rotateDeg: 0,
    }

    //绑定面板的参数监听处理
    function bindViewModel() {
      Cesium.knockout.track(viewModel) //面板更新监听

      // Bind the viewModel to the DOM elements of the UI that call for it.
      var toolbar = document.getElementById('toolbar')
      Cesium.knockout.applyBindings(viewModel, toolbar)

      // Make the active imagery layer a subscriber of the viewModel.
      function subscribeLayerParameter(name) {
        Cesium.knockout.getObservable(viewModel, name).subscribe(function (newValue) {
          updateParams()
        })
      }
      subscribeLayerParameter('aspectRatio')
      subscribeLayerParameter('dis')
      subscribeLayerParameter('fov')
    }

    function updateParams() {
      if (!selectedView) {
        return
      }

      selectedView.aspectRatio = Number(viewModel.aspectRatio)
      selectedView.dis = Number(viewModel.dis)
      selectedView.fov = Cesium.Math.toRadians(Number(viewModel.fov))
    }

    function playOrpause() {
      selectedView.play = !selectedView.play
    }

    function bindEvnet() {
      $('#rotateDeg').change(function () {
        if (!selectedView) {
          return
        }
        var num = $(this).val() * 1.0
        selectedView.stRotation = Cesium.Math.toRadians(num)
      })

      $('#showFrustum').click(function () {
        if (!selectedView) {
          return
        }
        var ckd = $(this)[0].checked
        selectedView.showFrustum = ckd
      })

      $('#btnSelCamera').click(function () {
        if (selectedView == null) {
          return
        }

        map.graphicLayer.startDraw({
          type: 'point',
          success: (graphic) => {
            var point = graphic.point
            graphic.remove() //删除绘制的点

            selectedView.position = point
          },
        })
      })

      $('#btnLocate').click(function () {
        if (!selectedView) {
          return
        }
        selectedView.flyTo()
      })
      $('#console').click(function () {
        if (!selectedView) {
          return
        }

        var params = selectedView.toJSON()
        console.log(JSON.stringify(params))
      })

      document.addEventListener(
        'keydown',
        function (e) {
          switch (e.keyCode) {
            default:
              break
            case 'A'.charCodeAt(0):
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.LEFT)
              break
            case 'D'.charCodeAt(0):
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.RIGHT)
              break
            case 'W'.charCodeAt(0):
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.TOP)
              break
            case 'S'.charCodeAt(0):
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.BOTTOM)
              break
            case 'Q'.charCodeAt(0): //Q键
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.ALONG)
              break
            case 'E'.charCodeAt(0): //E
              rotateCamera(mars3d.graphic.Video2D.RatateDirection.INVERSE)
              break
          }
        },
        false
      )
    }

    //微调视频
    function rotateCamera(dir) {
      if (!selectedView) {
        return
      }

      selectedView.rotateCamera(dir, $('#adjustDeg').val() * 1)

      var cameraFollow = $('#chkCameraFollow').is(':checked')
      if (cameraFollow) {
        map.camera.direction = selectedView.recordObj.direction
        map.camera.right = selectedView.recordObj.right
        map.camera.up = selectedView.recordObj.up
        map.camera.position = selectedView.position
      }
    }
  </script>
</body>

</html>
